#ifndef __ILIB_TEST_TP_PBN_H__
#define __ILIB_TEST_TP_PBN_H__

#include "../util/h/iLogger.h"
#include "../util/h/iTimer.h"
#include "../tp/iTPConnection.h"
#include "../tp/iTPListener.h"
#include "app.h"

class PBNTester  : public iTPConnection::iSink
                 , public iTPListener::iSink
                 , public iTimer::iSink
{
public:
    PBNTester(iTPConnection* conn)
    :m_conn(conn)
    {
		iTimer::new_instance(this, 2013, 200,  0);
    }
	
    void on_timer(iTimer* tmr)
    {
        switch(tmr->get_id())
        {
        case 2011:
            {
				if(m_conn==NULL)
				{
					tmr->release();
					return;
				}
                static uint32_t index=0;
                iOutputStream os;
                std::stringstream ss;
                ss<<"this is message from PBN client "<<index++;
                os << ss.str().c_str();
                m_conn->send_data(os.get_msg_db()->duplicate());
            }
            break;
        case 2012:
            {
				if(m_conn==NULL)
				{
					tmr->release();
					return;
				}
                static uint32_t index=0;
                iOutputStream os;
                std::stringstream ss;
                ss<<"this is message from PBN server "<<index++;
                os << ss.str().c_str();
                m_conn->send_data(os.get_msg_db()->duplicate());
            }
            break;
        case 2013:
            {
            }
            break;
        }
    }
    void on_connect_confirm(int32_t result, iMsgDB* rsp)
    {
        char local[64]={0}, remote[64]={0};
        m_conn->get_option(TP_OPT_LOCAL_ADDR,  local,  sizeof(local));
        m_conn->get_option(TP_OPT_REMOTE_ADDR, remote, sizeof(remote));
		INFO("PBN connect confirm result="<<result<<",local="<<local<<",remote="<<remote);
		INFO("PBN Connect confirm,msg length="<<rsp->get_total_data_length());
			
//        iTimer::new_instance(this, 2011, 200, 0);
    }
    void on_data_indication(iMsgDB* mbs)
    {
        INFO("on data indication,data:"<<mbs->dump());
    }
    void on_close(int32_t reason)
    {
        INFO("on close reason="<<reason);

        m_conn->release();
		m_conn = NULL;
    }

    virtual void on_connection_indication(iTPConnection* iConn, iMsgDB* req)
    {
        m_conn = iConn;
        char local[64]={0}, remote[64]={0};
        m_conn->get_option(TP_OPT_LOCAL_ADDR,  local,  sizeof(local));
        m_conn->get_option(TP_OPT_REMOTE_ADDR, remote, sizeof(remote));
        INFO("on connection indication, local="<<local<<",remote="<<remote);
		INFO("PBN Connect indication,msg len="<<req->get_total_data_length());

        iMsgDB* rsp = iMsgDB::new_instance(NULL, 64*1024-1, 64*1024-1);
        iConn->connect_response(rsp, this);
				
//        iTimer::new_instance(this, 2012, 200, 0);
    }
	
private:
    iTPConnection*  m_conn;
};

#endif //__ILIB_TEST_TP_PBN_H__
